package net.hzlj.platform.enterprise.receiver;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;

import net.hzlj.rl.zhangjiajie.R;
import net.hzlj.platform.enterprise.service.MainService;
import net.hzlj.platform.enterprise.utils.CommonUtils;
import net.hzlj.platform.enterprise.utils.ServiceUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * @Description:
 * @Author: Jason Zhang
 * @Date: 2017/1/17 0017
 */

public class LocationAppWidget extends AppWidgetProvider {

    public static final String TAG = LocationAppWidget.class.getSimpleName();

    //重新链接
    private Subscription mUpdateDateSubscription;
//    private AppWidgetManager mAppWidgetManager;
//    private int [] mAppWidgetIds;

    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
        Log.i(TAG, "onAppWidgetOptionsChanged appWidgetId:" + appWidgetId);
    }

    @Override
    public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        //每次更新都调用一次该方法，使用频繁
        Log.i(TAG, "onUpdate appWidgetIds:" + appWidgetIds.length);
//        mAppWidgetManager = appWidgetManager;
//        mAppWidgetIds = appWidgetIds;
        mUpdateDateSubscription = Observable.interval(0, 1000, TimeUnit.MILLISECONDS).filter(new Func1<Long, Boolean>() {
            @Override
            public Boolean call(Long aLong) {
//                Log.i(TAG, "mUpdateDateSubscription call:" + aLong);
                return appWidgetManager != null && appWidgetIds != null && appWidgetIds.length > 0;
            }
        }).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                Log.i(TAG, "mUpdateDateSubscription interval:" + aLong);
                final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_location_layout);
                remoteViews.setTextViewText(R.id.device_textView, "设备号码：" + CommonUtils.getDeviceID(context));
                remoteViews.setTextViewText(R.id.date_textView, "现在时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
                if (!ServiceUtils.isServiceRunning(context,
                        "net.hzlj.platform.enterprise.service.MainService")) {
                    context.startService(new Intent(context, MainService.class));
                }
            }
        });
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        //每接收一次广播消息就调用一次，使用频繁
        Log.i(TAG, "onReceive");
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        //每删除一个就调用一次
        Log.i(TAG, "onDeleted:" + appWidgetIds.length);
//        mAppWidgetIds = appWidgetIds;
        if (mUpdateDateSubscription != null) {
            mUpdateDateSubscription.unsubscribe();
        }
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        //当最后一个该Widget删除是调用该方法，注意是最后一个
        Log.i(TAG, "onDisabled");
        //重新链接
        if (mUpdateDateSubscription != null) {
            mUpdateDateSubscription.unsubscribe();
        }
    }

    @Override
    public void onEnabled(final Context context) {
        super.onEnabled(context);
        //当该Widget第一次添加到桌面是调用该方法，可添加多次但只第一次调用
        Log.i(TAG, "onEnabled");
    }

}
